package engine.ui.equipStrenGthen 
{
	import engine.control.NxTablePageTurner;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.View;
	import engine.logic.AlertLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.MessageUI;
	import engine.ui.NewItemChangeUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.comboBox.NxComboBoxSelect;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.table.NxTable;
	import newx.com.control.text.NxLabel;

	/**
	 * ...
	 * @author ...WangHaiBo 
	 * ...........www.woniu.com
	 * 装备保值逻辑
	 */
	public class EquipProtectionLogic implements IEquipLogic 
	{
		private static var _self:EquipProtectionLogic;
		private var viewgroup_old_6:NxViewGroup;
		private var viewgroup_new_6:NxViewGroup;
		private var table_left_6:NxTable;
		private var lab_uplevel_6:NxLabel;
		private var lab_succeedpercent_6:NxLabel;
		private var select_stonelevel_6:MovieClip;
		private var select_uplevel_6:MovieClip;
		private var lab_needmoney_6:NxLabel;
		private var btn_star_6:NxButton;
		private var lab_stonelevel_6:NxLabel;
		private var hole1_lab_6:NxLabel;
		private var lab_free_6:NxLabel;
		private var select_free_6:MovieClip
		
		private var _viewGroupArray:Array;
		private var _oldEquip:BaseObject;
		private var _newequip:BaseObject;
		private var _model:ModelLocator;
		
		private var _money : int;
		public function EquipProtectionLogic(val:MovieClip) 
		{
			_model					=	ModelLocator.getInstance();
			viewgroup_old_6			=	val.getChildByName("viewgroup_old_6") as NxViewGroup;
			viewgroup_new_6			=	val.getChildByName("viewgroup_new_6") as NxViewGroup;
			table_left_6			=	val.getChildByName("table_left_6") as NxTable;
			lab_uplevel_6			=	val.getChildByName("lab_uplevel_6") as NxLabel;
			lab_succeedpercent_6	=	val.getChildByName("lab_succeedpercent_6")as NxLabel;
			select_stonelevel_6		=	val.getChildByName("select_stonelevel_6") as MovieClip
			select_uplevel_6		=	val.getChildByName("select_uplevel_6") as MovieClip	
			lab_needmoney_6			=	val.getChildByName("lab_needmoney_6")as NxLabel;
			btn_star_6				=	val.getChildByName("btn_star_6")as NxButton;
			hole1_lab_6				=	val.getChildByName("hole1_lab_6") as NxLabel;
			lab_stonelevel_6		=	val.getChildByName("lab_stonelevel_6") as NxLabel;
			select_free_6			=	val.getChildByName("select_free_6") as MovieClip;
			lab_free_6				=	val.getChildByName("lab_free_6") as NxLabel;
			
			select_free_6.visible	=	false;
			lab_free_6.visible		=	false;
			_viewGroupArray			=	[null, viewgroup_old_6, viewgroup_new_6];

			select_free_6.gotoAndStop(1);
			select_stonelevel_6.gotoAndStop(1);
			select_uplevel_6.gotoAndStop(1);
			init();
		}
		
		public static function getInstance(dis:MovieClip):EquipProtectionLogic
		{
			if (_self == null) 
			{
				_self	=	new EquipProtectionLogic(dis);
			}
			return _self;
		}
		
		/* INTERFACE engine.ui.equipStrenGthen.IEquipLogic */
		/**
		 * 添加装备
		 * @param	val
		 */
		public function addEquip(val:BaseObject):void 
		{
			var viewIndex:int	=	int(val["ObjKey"]);
			var item:MovieClip	=	(_viewGroupArray[viewIndex] as NxViewGroup).getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			var equipClip:NxClip	=	new NxClip();
			equipClip.width			=	equipClip.height	=	40;
			equipClip.x				=	6;
			equipClip.y				=	5;
			if (viewIndex	== 1) 
			{
				_oldEquip	=	val;
			}
			else if (viewIndex	==	2)
			{
				_newequip	=	val;
			}
			val.HandleProperties("NowViewType",View.VIEWPORT_KEEPWORTH)
			item["data"]			=	val;
			equipClip.source		=	"res/items/icon_" + val.GetProperties("Photo") + ".jpg";
			item.addChild(equipClip);
			isReadyAll();
			//paintEquipPro();
			
		}
		/**
		 * 删除装备
		 * @param	val
		 */
		public function removeEquip(val:BaseObject):void 
		{
			var viewIndex:int	=	int(val["ObjKey"]);
			var item:MovieClip	=	(_viewGroupArray[viewIndex] as NxViewGroup).getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			item["data"]			=	null;
			if (viewIndex	==	1) 
			{
				_oldEquip	=	null
				
			}
			else if (viewIndex	==	2)
			{
				_newequip	=	null;
			}
			isReadyAll();
		}
		/**
		 * 清空所有的显示信息
		 */
		public function removeAll():void 
		{
			for each (var item:NxViewGroup in _viewGroupArray) 
			{
				if (item != null) 
				{
					var itemMc:MovieClip	=	item.getItemByIndex(0);
					while (itemMc.numChildren > 1)
					{
						itemMc.removeChildAt(1);
					}
					itemMc["data"]			=	null;
				}
			}
			table_left_6.dataProvider		=	null
			lab_needmoney_6.htmlText		=	"";
			lab_uplevel_6.htmlText			=	"";
			lab_succeedpercent_6.htmlText	=	"";
			btn_star_6.enabled				=	false;
			select_stonelevel_6.gotoAndStop(1);
			select_uplevel_6.gotoAndStop(1);
			hole1_lab_6.color					=	0xFF0000;
			lab_stonelevel_6.color				=	0xFF0000;
			select_stonelevel_6.mouseEnabled	=	false;
			select_uplevel_6.mouseEnabled		=	false;
			
		}
		/**
		 * 初始化
		 */
		private function init():void
		{
			addListener();
			_model		.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_KEEPWORTH_POINTS), keepWorthPoints);
		}
		/**
		 *  是否全部准备完成
		 */
		private function isReadyAll():void
		{
			lab_free_6.color	=	0x00FF40
			if (_oldEquip != null && _newequip != null) 
			{
				//是否可以保值空洞
				if (_oldEquip.CheckProperties("HoleCount")) 
				{
					if (_oldEquip.GetProperties("HoleCount") < 1) 
					{
						select_stonelevel_6.mouseEnabled	=	false;
						select_stonelevel_6.gotoAndStop(1);
						lab_stonelevel_6.color				=	0xFF0000;
					}
					else
					{
						select_stonelevel_6.mouseEnabled	=	true;
						select_stonelevel_6.gotoAndStop(2);
						lab_stonelevel_6.color				=	0x00FF40;
					}
				}
				else
				{
						select_stonelevel_6.mouseEnabled	=	false;
						select_stonelevel_6.gotoAndStop(1);
						lab_stonelevel_6.color				=	0xFF0000;
				}
				//是否要以保值等级
				if (_oldEquip.CheckProperties("IntensityLevel") && _newequip.CheckProperties("IntensityType")) 
				{
					if (Number(_oldEquip.GetProperties("IntensityLevel"))< 1 || Number(_newequip.GetProperties("IntensityType")) == 0) 
					{
						hole1_lab_6.color					=	0xFF0000;
						select_uplevel_6.mouseEnabled		=	false;
						select_uplevel_6.gotoAndStop(1);
					}
					else
					{
						hole1_lab_6.color					=	0x00FF40;
						select_uplevel_6.mouseEnabled		=	true;
						select_uplevel_6.gotoAndStop(2);
					}
				}
				else
				{
						hole1_lab_6.color					=	0xFF0000;
						select_uplevel_6.mouseEnabled		=	false;
						select_uplevel_6.gotoAndStop(1);
				}
			}
			else
			{
				select_stonelevel_6.mouseEnabled	=	false;
				select_uplevel_6.mouseEnabled		=	false;
				select_stonelevel_6.gotoAndStop(1);
				select_uplevel_6.gotoAndStop(1);
				hole1_lab_6.color					=	0xFF0000;
				lab_stonelevel_6.color				=	0xFF0000;
			}
			isChooseProtection()
		}
		/**
		 * 装备保值选项勾选是否全完成了
		 */
		private function isChooseProtection():void
		{
			if ((select_stonelevel_6.mouseEnabled == true || select_uplevel_6.mouseEnabled == true) &&
				(select_stonelevel_6.currentFrame == 2 || select_uplevel_6.currentFrame	==	2)) 
			{
				
				btn_star_6.enabled	=	true
			}
			else
			{
				btn_star_6.enabled	=	false;
			}
			/**
			 * 暂时不会，等武州重新策划
			if (select_uplevel_6.currentFrame == 2) 
			{
				if (!select_free_6.visible) 
				{
					select_free_6.gotoAndStop(2);
					select_free_6.visible	=	true;
					lab_free_6.visible		=	true
					select_free_6.enabled	=	true;
				}
			}
			else
			{
				select_free_6.visible	=	false;
				lab_free_6.visible		=	false
				select_free_6.enabled	=	false;
				select_free_6.gotoAndStop(2);
			}
			*/
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_kEEP_WORTH_POINT,
												select_stonelevel_6.currentFrame -1,
												select_uplevel_6.currentFrame -1);	
		}
		/**
		 * 保值信息
		 * @param	e
		 */
		private function keepWorthPoints(e:CustomMsgEvent):void
		{
			var needMoneyNum:int	=	e.pList[0];
			var upLevelNum:int		=	e.pList[1];
			if (select_uplevel_6.currentFrame	==	2) 
			{
				lab_uplevel_6.htmlText	=	"";
			}
			//是否显示特殊属性
			if (select_stonelevel_6.currentFrame == 2) 
			{
				paintEquipPro();
			}
			else
			{
				table_left_6.dataProvider	=	null
			}
			needMoney			=	needMoneyNum;
			upLevel				=	upLevelNum;
			succeedPercent		=	100;
		}
		/**
		 * 显示所需要的钱
		 */
		private function set needMoney(val:int):void
		{
			_money = val;
			var menoyStr:String	=	String(val)
			if (val == 0) 
			{
				lab_needmoney_6.htmlText		=	"";
			}
			else 
			{
				if (val <= _model.scene.Role.getMagicCoin()) 
				{
					menoyStr	=	"<font color='#00FF40'>"	+	menoyStr	+	"</font>"
				}
				else
				{
					menoyStr	=	"<font color='#FF0000'>"	+	menoyStr	+	"</font>"
				}
				lab_needmoney_6.htmlText		=	StaticIni.getSwfLangStrVar("F_Gem_13222", [menoyStr])	+	StaticIni.getSwfLangStr("Tips_CapitalType4");
			}

		}
		/**
		 * 成功率
		 */
		private function set succeedPercent(val:int):void
		{
			var succeedStr:String	=	"";
			if (_oldEquip == null || _newequip == null) 
			{
				succeedStr		=	"";
				lab_succeedpercent_6.htmlText	=	succeedStr;
			}
			else
			{
				succeedStr	=	"<font color='#00FF40'>100%</font>"
				lab_succeedpercent_6.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_36", [succeedStr]);
			}
			
		}
		/**
		 * 强化等级
		 */
		private function set upLevel(val:int):void
		{
			var levelStr:String	=	String(val);
			if (select_uplevel_6.currentFrame == 2) 
			{
				/**
				var oldEquipLevel:int	=	_oldEquip.GetProperties("IntensityLevel");
				var newEquipLevel:int	=	_newequip.GetProperties("IntensityLevel");
				if (oldEquipLevel < newEquipLevel)
				{
					levelStr	=	"<font color='#FF0000'>"	+	oldEquipLevel	+	"</font>"
				}
				else
				{
					levelStr	=	"<font color='#00FF40'>"	+	oldEquipLevel	+	"</font>"
				}
				*/
				lab_uplevel_6.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_42", [levelStr]);
			}
			else
			{
				lab_uplevel_6.htmlText	=	"";
			}

		}
		/**
		 * 将特殊属性写入表格中
		 */
		private function paintEquipPro():void
		{
			table_left_6.dataProvider	=	null;
			var itemArray: Array	=	[];
			var holdCountVal:String	=	"";
			var labelIndxt:int
			var label:String		=	"";
			var labelData:String	=	"";
			if (_oldEquip	==	null) return; 
			for (var i:int = 0; i < 4; i++) 
			{
				holdCountVal	=	_oldEquip.GetProperties("Jewel" + i);
				labelIndxt		=	13235	+ i;
				label			=	StaticIni.getSwfLangStr("F_Gem_" + labelIndxt);
				if (holdCountVal == "-1") 
				{
					labelData	=	"<font color='#00FF40'>"	+	StaticIni.getSwfLangStr("F_Gem_13234")	+	"</font>";
				}
				else if (holdCountVal == "0") 
				{
					labelData	=	"<font color='#FF0000'>"	+	StaticIni.getSwfLangStr("F_Gem_13245")	+	"</font>";
				}
				else
				{	
					labelData	=	"<font color='#FFFF00'>"	+	NewItemChangeUI.getInstance().readSpecialProperty(i,_oldEquip as ObjectData)	+	"</font>";
				}
				itemArray	=	[label,labelData]
				table_left_6.addLineByIndex(itemArray, -1, itemArray);
			}
		}
		/**
		 * 多选按钮事件
		 * @param	e
		 */
		private function selectClickHandle(e:MouseEvent):void
		{
			var clickSelectBtn:MovieClip	=	e.target as MovieClip;
			if (clickSelectBtn.currentFrame  == 1) 
			{
				clickSelectBtn.gotoAndStop(2);
			}
			else if (clickSelectBtn.currentFrame  == 2)
			{
				clickSelectBtn.gotoAndStop(1);
			}
			isChooseProtection();
		}
		/**
		 * 保值操作
		 */
		private function starBtnClickHandle(e:MouseEvent):void
		{
			if( _money > _model.scene.Role.getMagicCoin() && _newequip.GetProperties("CostType") != 1)
			{
				//提示，可以去商场买
				MessageUI.getInstance().showInfo( StaticIni.getSwfLangStr("80111") );
			}
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_KEEPWORTH_PROCESS,select_stonelevel_6.currentFrame -1,select_uplevel_6.currentFrame -1);
		}
		/**
		 * 添加事件
		 */
		private function addListener():void
		{
			select_free_6.addEventListener(MouseEvent.CLICK, 	selectClickHandle);
			select_stonelevel_6.addEventListener(MouseEvent.CLICK, 	selectClickHandle);
			select_uplevel_6.addEventListener(MouseEvent.CLICK, 	selectClickHandle);
			btn_star_6.addEventListener(MouseEvent.CLICK,	starBtnClickHandle);
		}
		/**
		 * 删除事件
		 */
		private function removeListener():void
		{
			select_free_6.removeEventListener(MouseEvent.CLICK, 	selectClickHandle);
			select_stonelevel_6.removeEventListener(MouseEvent.CLICK, 	selectClickHandle);
			select_uplevel_6.removeEventListener(MouseEvent.CLICK, 	selectClickHandle);
			btn_star_6.removeEventListener(MouseEvent.CLICK,	starBtnClickHandle);
		}
		
	}

}